/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Santiago Ontanon, Kinshuk Mishra
 												Neha Sugandh 
 												
Class							:				KillUnitGoal
 
Function						: 				Provides a class for holding the KillUnit goal
****************************************************************************/
package planninglanguage.goals;

import org.jdom.Element;

import planninglanguage.conditions.Condition;
import planninglanguage.conditions.NotCondition;
import planninglanguage.conditions.UnitTypeExistsCondition;
import planninglanguage.softconditions.NotSoftCondition;
import planninglanguage.softconditions.SoftCondition;
import planninglanguage.softconditions.UnitTypeExistsSoftCondition;
import util.XMLWriter;
import base.WargusGameState;

public class KillAllUnitsOfTypeGoal extends Goal implements Cloneable{
	int m_enemyID;
	String m_unitType;
	
	public KillAllUnitsOfTypeGoal(String a_unitType, int a_enemyID)
	{
		m_enemyID = a_enemyID;
		m_unitType = a_unitType;
	}
	
	public int unitID()
	{
		return m_enemyID;
	}
	
	public Object clone()
	{
		return new KillAllUnitsOfTypeGoal(m_unitType,m_enemyID);
	}
	
	public Condition generateSuccessCondition(WargusGameState a_gamestate)
	{
		UnitTypeExistsCondition uec = new UnitTypeExistsCondition(m_unitType,m_enemyID);
		return new NotCondition(uec);
	}
	
	public SoftCondition generateSuccessSoftCondition(WargusGameState a_gamestate)
	{
		UnitTypeExistsSoftCondition uec = new UnitTypeExistsSoftCondition(m_unitType,m_enemyID);
		return new NotSoftCondition(uec);
	}	
	
	public float distance(Goal a_g) {
		if (!(a_g instanceof KillAllUnitsOfTypeGoal)) return 1;
		
		KillAllUnitsOfTypeGoal g = (KillAllUnitsOfTypeGoal) a_g;
		float d_u = 1;
		float d_p = 1;

		if (m_unitType.equals(g.m_unitType)) d_u = 0;
		if (m_enemyID == m_enemyID) d_p = 0;
			
		return d_u*0.25f + d_p*0.25f;
	}	
	
	public String toString() {
		return "KillAllUnitsOfTypeGoal( type = " + m_unitType + ")";
	}
	
	public void writeXML(XMLWriter w) {
		w.tag(Goal.openTag + " " + Goal.XMLtype + "=\"" + 
				this.getClass().getSimpleName() + "\"");
		{
			w.tag("enemy-id", m_enemyID);
			w.tag("type", m_unitType);
		}
		w.tag(Goal.closeTag);
	}
	protected static KillAllUnitsOfTypeGoal loadFromXML(Element e) {
		String type = e.getChildText("type");
		int enemyID = Integer.parseInt(e.getChildText("enemy-id"));
		return new KillAllUnitsOfTypeGoal(type, enemyID);
	}
	/* <goal>  <name>destroy-unit</name>  <playerID>1</playerID>  <type>greathall</type></goal> */ 
}

